/*
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.dialog.wizard;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.event.AncestorEvent;

import org.apache.log4j.Logger;

import com.wateray.ipassbook.util.log4j.Log;

/**
 * @author wateray
 * @create 2009-5-16
 */
public abstract class AbstractWizardPage extends JPanel implements IWizardPage{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	protected Logger logger;
	
	protected static int top = 5;
	protected static int left = 15;
	protected static int bottom = 5;
	protected static int right = 5;
	protected static Insets inserts = new Insets(top, left, bottom, right);
	
	protected static int gridx = 0;
	protected static int gridy = 0;
	
	protected JLabel pageNameLabel;
	protected GridBagLayout gridbag;
	protected GridBagConstraints constraints;
	
	//IWizardPage page
	protected IWizardPage previousPage;

	//
	protected IWizardDialog wizardDialog;
	
	public AbstractWizardPage(IWizardDialog wizardDialog){
		//default page name.
		setPageName(this.getClass().getSimpleName());
		logger = Log.getLogger(this.getClass());
		
		this.wizardDialog = wizardDialog;
		this.addAncestorListener(this);
		
		initPage();
	}
	
	protected void initPage(){
		gridbag = new GridBagLayout();
		constraints = new GridBagConstraints();
		this.setLayout(gridbag);
		
		//getPassbookNameLabel
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor=GridBagConstraints.ABOVE_BASELINE_LEADING;

		//pageNameLabel
		constraints.insets =  new Insets(top, left, 0, right);
		constraints.gridx = GridBagConstraints.RELATIVE;
		constraints.gridy = gridy;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		this.add(getPageNameLabel(), constraints);
		
		//separator.
		constraints.insets =  new Insets(0, left, bottom, right);
		constraints.gridy = ++gridy;
		this.add(new JSeparator(), constraints);
		
		//default.
		constraints.gridx = 0;
		constraints.gridwidth = 1;
		
	}
	protected JLabel getPageNameLabel(){
		if(pageNameLabel == null){
			pageNameLabel = new JLabel();
			//get page name.
			pageNameLabel.setText(this.getName());
		}
		return pageNameLabel;
	}
	
	/**
	 * @param page -- IWizardPage
	 * */
	@Override
	public void setPreviousPage(IWizardPage page) {
		// TODO Auto-generated method stub
		previousPage = page;
	}
	
	/**
	 * @return wizardDialog -- IWizardDialog
	 * */
	@Override
	public IWizardDialog getWizardDialog(){
//		Component component = this.getParent();
//		if(component instanceof IWizardDialog){
//			return (IWizardDialog) component;
//		}
		return wizardDialog;
	}
	
//	public void setWizardDialog(IWizardDialog wizardDialog){
//		this.wizardDialog = wizardDialog;
//	}

	@Override
	public String getPageName(){
		return this.getName();
	}
	
	@Override
	public void setPageName(String pageName) {
		// TODO Auto-generated method stub
		this.setName(pageName);
	}
	
	@Override
	public void ancestorAdded(AncestorEvent event){
		this.beforeShowing();
	}
	
	@Override
	public void ancestorRemoved(AncestorEvent event){
		
	}
	
	@Override
	public void ancestorMoved(AncestorEvent event){
		
	}
}
